<?php
/**
 * 商户
 * User: chen guang
 * Date: 2017/9/29 9:00
 *
 */

namespace app\admin\Controller;


use think\Db;
use think\Request;

class Set extends Common{
	
	/*
	*	构造函数
	*/
	function __construct(){
		parent::__construct();

	}
	/**
	 * 设置
	 */
	public function index(){
		$list=db("config")->select();
		$this->assign('list',$list);
		return $this->fetch();
	}
	/**
	 * 设置
	 */
	public function deal(){
		$name=input('name');
		$value=input('value');
		if(!$name){echo -1;exit;};
		$where['id']=$name;
		$list=db("config")->where($where)->find();
		if(!$list) {echo -2;exit;};
		$res=db("config")->where($where)->update(array('value'=>$value));
		if(!$res) {echo -3;exit;};
		echo 1;exit;
	}
	/**
	 * 关于我们
	 */
	public function about(){
		$about = db('about');
		$ban = $about->find();
		if(request()->isPost()){
			$data['content']=input('content');
			$data['title']=input('title');
			$data['des']=input('des');
			$file=request()->file('image');
			if($file){
				$info = $file->move(ROOT_PATH . 'public' . DS . 'static' . DS . 'uploads');
				if($info){
					$data['image']= $info->getSaveName();
				}
			}
			$res = $about ->where("id=".$ban['id'])->update($data);
			if($res!==false){$this->success('操作成功');}else{$this->success('操作失败');}
		}
		$this -> assign("data",$ban);
		return $this->fetch();
	}

	/**
	 * 分类详情
	 */
	public function cdetail(){
		$id = input('id/d');
		$model = db('category');
		if(empty($id)){
			$category = $model->where(array('id' => $id))->find();
			if(!$category)$this->error('不存在的分类');
			$this->assign('res',$category);
		}
		$where['typeid']=array('neq',$id);
		$where['parent_id']=0;
		$pcates = $model->where($where)->select();
		$this->assign('pcates',$pcates);
		return $this->fetch();
	}
	/**
	 * 图文列表
	 */

	public function article(){
		$db = db('article');
		$article = $db ->paginate(10);
		$this -> assign("res",$article);
		return $this->fetch();
	}
	/**
	 * 系统信息列表
	 */

	public function news(){
		$db = db('news');
		$article = $db ->paginate(10);
		$this -> assign("res",$article);
		return $this->fetch();
	}
	/**
	 * 图文详情
	 */
	public function detail(){
		$id=input('id');
		$about = db('article');
		$ban = $about->where("id='$id'") ->find();
		if(request()->isPost()){
			$data['content']=input('content');
			$data['title']=input('title');
			$data['status']=input('status');
			$data['cid']=input('cid');
			$data['sort']=input('sort');
			$file=request()->file('image');
			if($file){
				$info = $file->move(ROOT_PATH . 'public' . DS . 'static' . DS . 'uploads');
				if($info){
					$data['image']= $info->getSaveName();
				}
			}
			if($id) {
				$res = $about->where("id=" . $ban['id'])->update($data);
			}else{
				$res = $about->insert($data);
			}
			if($res!==false){$this->success('操作成功');}else{$this->success('操作失败');}
		}
		$this -> assign("data",$ban);
		return $this->fetch();
	}
	/**
	 * 系统信息详情
	 */
	public function newsdetail(){
		$id=input('id');
		$about = db('news');
		$ban = $about->where("id='$id'") ->find();
		if(request()->isPost()){
			$data['content']=input('content');
			$data['title']=input('title');
			$data['des']=input('des');
			$data['create_time']=NOW_TIME;
			if($id) {
				$res = $about->where("id=" . $ban['id'])->update($data);
			}else{
				$id = $about->insertGetId($data);
				$where['status']=1;
				$where['delete']=1;
				$member=db("member")->where($where)->select();
				$n_user = db('news_user');
				foreach($member as $v){
					$dd['uid']=$v['id'];
					$dd['nid']=$id;
					$res=$n_user->insert($dd);
				}
			}
			if($res!==false){$this->success('操作成功');}else{$this->success('操作失败');}
		}
		$this -> assign("data",$ban);
		return $this->fetch();
	}
	/**
	 * 系统信息删除
	 */
	public function news_del(){
		$bnid = input('get.id');
		$banner = db('news');
		$del = $banner -> where("id='$bnid'") -> delete();
		$this->success('操作成功');
	}
	/**
	 * 轮播图
	 */
	public function banner(){
		$banner = db('banner');
		$ban = $banner->select();
		$this -> assign("res",$ban);
		return $this->fetch();
	}
	/**
	 * 轮播添加或修改
	 */
	public function banner_edit(){
		$id = input('id');
		if(Request::instance()->isPost()){
			$data['status'] = input('post.status');
			$data['title'] = input('post.title');
			$data['displayorder'] = input('post.displayorder');
			$data['url'] = input('post.url');
			$file = request()->file('bnimage');
			if (!empty($file)) {
				$info = $file->move(ROOT_PATH . 'public/static/uploads');
				if ($info) {
					$data['pic'] = $info->getSaveName();
				} else {
					$this->success($file->getError());
				}
			} else {
				$data['pic'] = input('bne');
			}
			if($id){
				$bnr = db('banner')->where("id='$id'")->update($data);
			}else{
				$bnr = db('banner')->insert($data);
			}
			$this->success("操作成功!",url('Set/banner'));
		}
		$banner=db('banner')->where("id='$id'")->find();
		$this -> assign("banner",$banner);
		return $this->fetch();

	}
	/**
	 * 轮播删除
	 */
	public function banner_del(){
		$bnid = input('get.id');
		$banner = db('banner');
		$del = $banner -> where("id='$bnid'") -> delete();
		$this -> success("删除成功",url('Set/banner'));
	}
}